# -*- coding: utf-8 -*-

from django.contrib.auth.models import User
import models
import random

# -------------------

def get_my_forms(id):
    myforms_f = []
    for myform in models.MyForm.objects.filter(user = id):
        myforms_f.append( [myform.id, str(myform)] )
    return myforms_f

# -------------------

def new_my_form(user_id, new_title):
    the_user = User.objects.filter(id = user_id)[0]
    myform = models.MyForm(user = the_user, title = new_title)
    myform.save()

# -------------------

def myform_exists(myform_id, user_id):
    myforms = models.MyForm.objects.filter(id = myform_id, user = user_id)
    return (len(myforms) > 0)

# -------------------

def question_exists(q_id):
    questions = models.Question.objects.filter(id = q_id)
    return (len(questions) > 0)

# -------------------

def number_of_questions(myform_id):
    the_form = models.MyForm.objects.filter(id = myform_id)[0]
    return len(models.Question.objects.filter(myform = the_form))

# -------------------

def number_of_answers(question_id):
    the_question = models.Question.objects.filter(id = question_id)[0]
    return len(models.Answer.objects.filter(question = the_question))
# -------------------

def get_questions_for_edit(myform_id):
    the_form = models.MyForm.objects.filter(id = myform_id)[0]
    questions = []
    for q in models.Question.objects.filter(myform = the_form):
        questions.append( [q.id, str(q)] )
    return questions

# -------------------

def get_my_form_title(myform_id):
    return str(models.MyForm.objects.filter(id = myform_id)[0])

#-------------------- 

def get_all_forms(u_id):
    forms = []
    stats = models.Stats.objects.filter(user = u_id)
    answered_forms = []
    for i in stats:
        answered_forms.append(i.myform.id)
    for form in models.MyForm.objects.all():
        if answered_forms.count(form.id) > 0:
            forms.append((form.id, str(form), "(Contestado previamente)"))
        else:
            forms.append((form.id, str(form), " "))
    return forms

#--------------------

def add_question(myform_id, question_text, answer_text):
    the_form = models.MyForm.objects.filter(id = myform_id)[0]
    new_question = models.Question(myform = the_form, text = question_text)
    new_question.save()
    correct_answer = models.Answer(question = new_question, text = answer_text, valor = True)
    correct_answer.save()

#--------------------

def get_question_text(q_id):
    return str(models.Question.objects.filter(id = q_id)[0])

#--------------------

def get_answer_text(answer_id):
    return str(models.Answer.objects.filter(id = answer_id)[0])

#--------------------

def get_nice_answers(question_id):
    the_question = models.Question.objects.filter(id = question_id)[0]
    lst = []
    correct = models.Answer.objects.filter(question = the_question, valor = True)[0]
    lst.append( [correct.id, str(correct)] )
    lst.append( [ [ans.id, str(ans)] for ans in models.Answer.objects.filter(question = the_question, valor = False) ] )
    return lst
   
#-------------------- 

def add_answer(question_id, answer_text):
    the_question = models.Question.objects.filter(id = question_id)[0]
    new_answer = models.Answer(question = the_question, text = answer_text, valor = False)
    new_answer.save()
    
#--------------------

def delete_answer(answer_id):
    answer = models.Answer.objects.filter(id = answer_id)[0]
    answer.delete()

#--------------------

def delete_question(question_id):
    question = models.Question.objects.filter(id = question_id)[0]
    question.delete()

#--------------------

def delete_my_form(myform_id):
    myform = models.MyForm.objects.filter(id = myform_id)[0]
    myform.delete()

#-------------------- 

def set_answer_text(a_id, new_text):
    answer = models.Answer.objects.filter(id = a_id)[0]
    answer.text = new_text
    answer.save()

#---------------------

def set_question_text(q_id, new_text):
    question = models.Question.objects.filter(id = q_id)[0]
    question.text = new_text
    question.save()

#---------------------

def set_my_form_text(myform_id, new_text):
    myform = models.MyForm.objects.filter(id = myform_id)[0]
    myform.title = new_text
    myform.save()

#---------------------

def change_valid_answer(question_id, answer_id):
    answers = models.Answer.objects.filter(question = question_id)
    for answer in answers:
        answer.valor = False
        answer.save()
    
    new_valid_answer = models.Answer.objects.filter(id = answer_id)[0]
    new_valid_answer.valor = True
    new_valid_answer.save()

#-------------------- 

def get_questions(f_id):
    question_list = []
    f_id_questions = models.Question.objects.filter(myform = f_id)
    for question in f_id_questions:
        question_list.append((question.text,question.id))
    return question_list[::-1]
    
#--------------------

def shuffle(lis):
    random_list = [(random.random(), x) for x in lis]
    random_list.sort()
    return [x for (r, x) in random_list]

#--------------------

def get_answer_list(q_id):
    answers = models.Answer.objects.filter(question=q_id)
    return shuffle(answers)

#--------------------
    
def is_correct(a_id):
    answer = models.Answer.objects.filter(id = a_id)[0]
    if answer.valor:
        return True
    else:
        return False
    
#--------------------

def get_correct_answer(q_id):
    answer_list = models.Answer.objects.filter(question = q_id)
    for answer in answer_list:
        if is_correct(answer.id):
            return answer.text
            
#---------------------

def add_stats(f_id, u_id, total_questions, total_correct):
    the_user = models.User.objects.filter(id = u_id)[0]
    the_form = models.MyForm.objects.filter(id = f_id)[0]    
    new_stats = models.Stats(user = the_user, myform = the_form, questions_total = total_questions, questions_correct =  total_correct)
    new_stats.save()  
    
#---------------------

def stat_per_form(u_id, f_id):
    the_user = models.User.objects.filter(id = u_id)[0]
    the_form = models.MyForm.objects.filter(id = f_id)[0]
    
    form_tries = [str(the_form)]
    for form in models.Stats.objects.filter(user=the_user, myform=the_form).order_by('date')[::-1]:
        data=[]
        data.append(str(form.date))
        data.append("Correctas:%s/%s" % (form.questions_correct,form.questions_total) )
        average = (form.questions_correct*100)/form.questions_total
        data.append("%s" % (average,) )
        form_tries.append(data)
        
    return form_tries

def get_forms_from_user(u_id):
    form_ids = []
    form_list = models.Stats.objects.filter(user = u_id)
    for i in form_list:
        form_ids.append(i.myform.id)
    form_ids.sort()
    
    unique_f_ids = []
    
    for i in form_ids:
        if unique_f_ids.count(i) < 1:
            unique_f_ids.append(i)
            
    total = [ stat_per_form(u_id,x) for x in unique_f_ids  ]

    vegetuplas = []
    for forma in total:
        vegetuplas.append( (forma[0], forma[1:]) )

    return vegetuplas

